package com.cennavi.minenavidemo.activity.navi;

import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.NaviPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.utils.ScreenUtil;
import com.cennavi.minenavidemo.utils.SelectNaviPointCallBack;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 地图上选择导航点
 */
public class SelectNaviPointFragment extends BaseFragment implements PoiPresenter.OnPoiSearchListener {
	private static final String TAG = "SelectNaviPointFragment";

	private Context mContext;
	private View mContentView;
	private Unbinder mUnbinder;
	private int mScreenWidth;
	private int mSelectPointType;
	private MapPresenter mMapPresenter;
	private NaviPresenter mNaviPresenter;
	private PoiPresenter mPoiPresenter;
	private PoiItem mPoiItem;
	private SelectNaviPointCallBack mCallBack;

	@BindView(R.id.rl_positionIconContainer)
	RelativeLayout rl_positionIconContainer;
	@BindView(R.id.iv_positionIcon)
	ImageView iv_positionIcon;
	@BindView(R.id.rl_selectNaviPointOnMapContainer)
	RelativeLayout rl_selectNaviPointOnMapContainer;

	@BindView(R.id.ll_pointInfoContainer)
	LinearLayout ll_pointInfoContainer;
	@BindView(R.id.ll_bottom_view)
	LinearLayout ll_bottom_view;
	@BindView(R.id.tv_loading)
	TextView tv_loading;
	@BindView(R.id.ll_pointInfo)
	LinearLayout ll_pointInfo;
	@BindView(R.id.tv_area)
	TextView tv_area;
	@BindView(R.id.tv_address)
	TextView tv_address;
	@BindView(R.id.tv_selectPoint)
	TextView tv_selectPoint;

	@Override
	public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {

	}

	@Override
	public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

	}

	@Override
	public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {
		tv_loading.setVisibility(View.INVISIBLE);
		ll_pointInfo.setVisibility(View.VISIBLE);

		List<PoiItem> poiItems = regeocodeAddress.getPois();

		if (poiItems != null && poiItems.size() > 0) {
			PoiItem poiItem = poiItems.get(0);

			tv_area.setText(poiItem.title);
			mPoiItem = poiItem;
			String poiAddress = poiItem.snippet;

			if (TextUtils.isEmpty(poiAddress) || poiAddress.equals(" ")) {
				tv_address.setText(poiItem.title + "附近");
			} else {
				tv_address.setText(poiItem.snippet);
			}
		}
	}

	@Override
	public void onDistrictSearch(DistrictItem districtItem) {

	}

	@Override
	public void onGetInputtips(List<Tip> tips) {

	}

	public static class SelectPointType {
		public static final int startNaviPoint = 1;
		public static final int endNaviPoint = 2;
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.select_navi_point_fragment, container, false);
		initData();
		initView();
		return mContentView;
	}

	@Override
	public void onDestroyView() {
		if (iv_positionIcon != null) {
			iv_positionIcon.removeOnAttachStateChangeListener(mOnAttachStateChangeListener);
		}
		super.onDestroyView();
		mUnbinder.unbind();
	}

	@Override
	public void initData() {
		mContext = getActivity();
		mMapPresenter = MapPresenter.getInstance();
		mNaviPresenter = NaviPresenter.getInstance();
		mPoiPresenter = PoiPresenter.getInstance();
		mPoiPresenter.setOnPoiSearchListener(this);
		mScreenWidth = ScreenUtil.getInstance().getScreenWidth();
	}

	@Override
	public void initView() {
		mUnbinder = ButterKnife.bind(this, mContentView);
		iv_positionIcon.addOnAttachStateChangeListener(mOnAttachStateChangeListener);
	}

	View.OnAttachStateChangeListener mOnAttachStateChangeListener = new View.OnAttachStateChangeListener() {
		@Override
		public void onViewAttachedToWindow(View v) {
			iv_positionIcon.post(new Runnable() {
				@Override
				public void run() {
					if (iv_positionIcon != null) { //  界面销毁的时候会置null 判断一下
						ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) iv_positionIcon.getLayoutParams();
						marginLayoutParams.bottomMargin = iv_positionIcon.getHeight() / 2 + 100;
						iv_positionIcon.setLayoutParams(marginLayoutParams);
					}
				}
			});
		}

		@Override
		public void onViewDetachedFromWindow(View v) {

		}
	};

	@Override
	public void fitStatusBar(int statusBarHeight) {
	}

	public void resetView(int selectPointType) {
		mSelectPointType = selectPointType;
		int positionIconResId = 0;
		String tvSelectPoint = "";
		iv_positionIcon.setVisibility(View.VISIBLE);
		if (selectPointType == SelectPointType.startNaviPoint) {
			positionIconResId = R.drawable.select_navi_point_start;
			tvSelectPoint = mContext.getResources().getString(R.string.setAsStarPoint);
		} else if (selectPointType == SelectPointType.endNaviPoint) {
			positionIconResId = R.drawable.select_navi_point_end;
			tvSelectPoint = mContext.getResources().getString(R.string.setAsEndPoint);
		}
		iv_positionIcon.setImageResource(positionIconResId);
		tv_selectPoint.setText(tvSelectPoint);
		showLoadingView();
		startSelect();
	}

	public void showLoadingView() {
		ll_pointInfo.setVisibility(View.INVISIBLE);
		tv_loading.setText("地图上的点");
		tv_loading.setVisibility(View.VISIBLE);
	}

	public void startSelect() {
		tv_loading.setText("加载中...");

		Point point = mMapPresenter.getMineMap().getWorldCenter();
		mPoiPresenter.reGeocode(Tools.pointToLatLonPoint(point));
	}

	// 设置 接口回调 方法
	public void sendMessage(SelectNaviPointCallBack callBack) {
		mCallBack = callBack;
	}

	@OnClick(R.id.tv_selectPoint)
	public void selectPoint() {
		String name = "";

		if (tv_loading.getVisibility() == View.VISIBLE) {
			name = "地图上的点";
		} else {
			name = tv_area.getText().toString();
		}

		mCallBack.mapPoint(mPoiItem, mSelectPointType);
	}
}
